package com.patent.controller;

import com.patent.common.JsonUtil;
import com.patent.common.ResultCode;
import com.patent.common.ResultData;
import com.patent.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {

	private static final Logger log = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	/**
	 *  校验验证码，保存用户注册信息
	 * @param source 来源 1：钉钉 2：微信小程序
	 * @param userMobile 用户注册手机号
	 * @param authCode 验证码
	 */
	@RequestMapping(value = "/saveUserInfo.do", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String saveUserInfo(String source, String userMobile, String authCode){
		log.info("saveUserInfo param:"+ source + "|" + userMobile + "|" +authCode);
		ResultData resultData = new ResultData();
		if(StringUtils.isBlank(userMobile)){
			resultData.setCode(ResultCode.paramError.getCode());
			resultData.setMsg("userMobile is null");
			return JsonUtil.write2JsonStr(resultData);
		}
		try {
			resultData = userService.saveUserInfo(source,userMobile, authCode);
		} catch (Exception e) {
			resultData.setCode(ResultCode.otherError.getCode());
			resultData.setMsg(ResultCode.otherError.getMessage());
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.info("saveUserInfo response:"+JsonUtil.write2JsonStr(resultData));
		return JsonUtil.write2JsonStr(resultData);
	}

}
